<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\wh\model;

use think\Model;
use app\admin\model\Config as ConfigModel;
use app\wh\model\StockType as StockTypeModel;
use app\wh\model\Color as ColorModel;
use think\Db;

/**
 * 前端接口模型
 */
class App extends Model
{

    //app页面
    public static $page = [
        //1级
        'wh'                =>  '仓库',
        'paint'             =>  '油漆库',
        'paste'             =>  '色浆库',
        'thinoil'           =>  '稀油库',
        'stone'             =>  '仿石材库',
        'woodpa'            =>  '木纹纸库',
        'section'           =>  '型材库',
        'lvcoil'            =>  '铝卷库',
        'lvsheet'           =>  '铝卷库',
        'chemical'          =>  '化工',
        'other'             =>  '其他',

        //2级
        'join'              =>  '入库',//通用
        'out'               =>  '出库',//通用
        'inventory'         =>  '盘存',//通用
        'detail'            =>  '库存',//通用
        'direct_join'       =>  '外协入库',//通用
        'direct_out'        =>  '外协出库',//通用
        'direct_also'       =>  '外协退还',//通用

        'join_p'            =>  '入库',//采购单入库，带打印
        'out_p_order'       =>  '按工程领用',//选择档案号，并展示已确认颜色
        'out_p_color'       =>  '按颜色领用',//扫码后，进去页面，选择档案号，批量领用
        'also_p'            =>  '扫码退还',//退还油漆类（油漆，仿石材）
        'also_p_order'      =>  '工程退还',//选择档案号，按领用量退还

        'join_t_color'      =>  '色号入库',//搜索厂家色号，并展示
        'join_t_order'      =>  '工程入库',//选择档案号，并展示

        'join_lc'           =>  '入库',//采购单入库，铝卷
        'out_lc'            =>  '出库',//出库，铝卷

        'join_lb'           =>  '入库',//采购单入库，铝板
        'out_lb'            =>  '出库',//出库，铝板
        'inventory_lb'      =>  '盘存',//盘存，铝板

    ];
    // 配置app菜单
    public static function getMenu($pid = 0,$user = []){

        $menus = [];

        if(!empty($pid)){

            $page = self::$page;
            $menu = ['join','out','inventory','detail'];

            switch ($pid){
                case 4://铝卷库
                    $menu = ['join_lc','out_lc','inventory','detail'];
                    break;
                case 5://铝板库
                    $menu = ['join_lb','out_lb','inventory_lb','detail'];
                    break;
                case 8://稀油库
                    $menu = ['join_t_color','join_t_order','out_p_order','out_p_color','also_p','also_p_order','inventory','direct_join','direct_out','direct_also','detail'];
                    break;
                //case 10://水性漆
                //    $menu = ['join_p','out_p_order','also_p','also_p_order','inventory'];
                //    break;
                //油漆库
                case in_array($pid,ColorModel::$type_pid_arr):
                    $menu = ['join_p','out_p_order','out_p_color','also_p','also_p_order','inventory','direct_join','direct_out','direct_also','detail'];
                    break;
            }

            if(!empty($menu)){
                foreach ($menu as $m){
                    $menus[] = [
                        'id'        =>  0,
                        'pid'       =>  $pid,
                        'page'      =>  $m,
                        'title'     =>  $page[$m],
                        'menu'      =>  [],
                    ];
                }
            }

        }
        else{

            $where = [];
            if(!empty($user['role'])){
                //获取角色权限
                $ids = Db::name('admin_role')->where(['id'=>$user['role']])->value('stock_type_ids');
                if(!empty($ids) && empty($pid))$where[] = ['id','in',$ids];
            }

            $stockType = StockTypeModel::getGroup(0,false,$where);

            foreach ($stockType as $k=>$v){
                //一级菜单
                $menu = [
                    'id'        =>  $k,
                    'pid'       =>  0,
                    'page'      =>  'wh',
                    'title'     =>  $v,
                    'menu'      =>  self::getMenu($k),//二级菜单
                ];

                $menus[] = $menu;
            }
        }

        return $menus;
    }

}
